<template>
  <PageWrapper
    title="任务发布"
    contentBackground
    content=" 表单页用于向用户发起任务。"
    contentClass="p-4"
  >
    <BasicForm @register="register" />
  </PageWrapper>
</template>
<script lang="ts">
  import { BasicForm, useForm } from '/@/components/Form';
  import { defineComponent ,computed,onMounted} from 'vue';
  import { schemas } from './data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { useUserStore } from '/@/store/modules/user';
  import { AffectedRowsResultModel } from '/@/api/learningcorner/model/userModel';
  import { RealeseTask } from '/@/api/learningcorner/task';


  export default defineComponent({
    components: { BasicForm, PageWrapper },
    setup() {
      const userStore = useUserStore();
      const realName = computed(() => {
      const { realName } = userStore.getUserInfo || {};
        return realName;
      });
      const { createMessage } = useMessage();
      const [register, {validate,setProps,getFieldsValue,setFieldsValue }] = useForm({
        labelCol: {
          span: 7,
        },
        wrapperCol: {
          span: 10,
        },
        schemas: schemas,
        actionColOptions: {
          offset: 8,
        },
        submitButtonOptions: {
          text: '提交',
        },
        showResetButton:false,
        submitFunc: customSubmitFunc,
      });


      onMounted(()=>{
        SetUserID();
    });

      async function SetUserID() {
        setFieldsValue({
          id: new Date().getTime(),
          jrid: realName.value,
        });

      }

      async function customSubmitFunc() {
        await validate()
        try {
           const values = getFieldsValue();
           console.log(values)
            let result: AffectedRowsResultModel = await RealeseTask(values);
          setProps({
            submitButtonOptions: {
              loading: true,
            },
          });
          setTimeout(() => {
            setProps({
              submitButtonOptions: {
                loading: false,
              },
            });
            if(result.affectedRows==1){
              createMessage.success('提交成功！');
            }
            else{
              createMessage.error('提交失败');
            }

          }, 2000);
        } catch (error) {}
      }

      return { register,realName };
    },
  });
</script>
<style lang="less" scoped>
  .form-wrap {
    padding: 24px;
    background-color: @component-background;
  }
</style>
